package cn.appsys.controller.deve;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.constants.Constants;
import cn.appsys.pojo.DevUser;
import cn.appsys.service.deve.user.DevUserService;

/**
 * 登陆控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/devLogin")
public class DevLoginController {
	
	@Resource
	private DevUserService devUserServiceImpl;
	
	/**
	 * 只负责跳转到开发者登陆页面
	 * @return
	 */
	@RequestMapping("/toDevLogin")
	public String toDevLogin(){
		return "devlogin";
	}
	
	/**
	 * 处理登陆，账号密码正确就跳转到开发者平台
	 * @return
	 */
	@RequestMapping("/doDevLogin")
	public String doDevLogin(@RequestParam("devCode") String devCode,
							   @RequestParam("devPassword") String devPassword,
							   HttpSession session,Model model){
		DevUser user = devUserServiceImpl.queryDevCodeAndDevPassword(devCode);
		if (user==null) {
			model.addAttribute("error", "账号不存在");
			return "devlogin";
			//throw new RuntimeException("账号不存在");
		}else if (!user.getDevPassword().equals(devPassword)) {
			model.addAttribute("error", "密码错误");
			return "devlogin";
		}
		//保存登陆用户信息
		session.setAttribute(Constants.DEV_USER_SESSION, user);
		return "redirect:/DevUser/tomain";
	}
	
	/**
	 * 用户登出—跳回index.jsp
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session){
		session.removeAttribute(Constants.DEV_USER_SESSION);
		session.removeAttribute(Constants.USER_SESSION);
		//跳转到WebContent下的index.jsp文件夹
		return "redirect:/./index.jsp";//redirect:/devLogin/toDevLogin
	}
}
